package com.pocket.demo.base.enums;


import com.google.common.collect.Sets;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;

import java.util.Set;

/**
 * 消息状态枚举
 */
public enum MsgCodeEnum {
    SUCCESS((long) HttpServletResponse.SC_OK, "success"),
    ERROR((long) HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "error"),
    BAD_REQUEST((long) HttpServletResponse.SC_BAD_REQUEST, "Bad Request"),
    UN_AUTHORIZED((long) HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"),
    REQ_REJECT((long) HttpServletResponse.SC_FORBIDDEN, "Forbidden"),
    NOT_FOUND((long) HttpServletResponse.SC_NOT_FOUND, "Not Found"),

    ERROR_UNAUTHENTICATED((long) HttpServletResponse.SC_UNAUTHORIZED, "error/unauthenticated"),
    ERROR_UNAUTHORIZED((long) HttpServletResponse.SC_UNAUTHORIZED, "error/unauthorized"),



    ERROR_NOT_NULL(100001L, "error/not-null"),
    ERROR_NOT_FOUND(100002L, "error/not-found"),
    ERROR_EXISTS(100003L, "error/exists"),
    ERROR_NOT_EXISTS(100004L, "error/not-exists"),
    ERROR_BAD_PARAMETER(100005L, "error/bad-parameter"),
    ERROR_VALID_BAD_PARAMETER(100006L, "error/valid-bad-parameter"),
    ERROR_EXCEPTION_HAPPEN(100007L, "error/exception-happen"),
    ERROR_CSRF_NOT_MATCH(100008L, "error/csrf-not-match"),
    ERROR_USERNAME_PASSWORD_ERROR(10009L, "error/username-password-error"),
    ERROR_ACCESS_TOKEN_EXPIRED(10010L, "error/access-token-expired"),
    ERROR_USER_DISABLED(10011L, "error/user-disabled"),

    ;

    private Long code;
    private String msgKey;
    MsgCodeEnum(Long code, String msgKey) {
        this.code = code;
        this.msgKey = msgKey;
    }

    public Long getCode() {
        return this.code;
    }

    public void setCode(Long code) {
        this.code = code;
    }

    public String getMsgKey() {
        return this.msgKey;
    }

    public void setMsgKey(String msgKey) {
        this.msgKey = msgKey;
    }


    private static Set<Long> HTTP_STATUS_OK;
    private static Set<Long> HTTP_STATUS_FORBIDDEN;
    private static Set<Long> HTTP_STATUS_NOT_FOUND;
    private static Set<Long> HTTP_STATUS_UNAUTHORIZED;

    static {
        HTTP_STATUS_FORBIDDEN = Sets.newHashSet(new Long[] {  });
        HTTP_STATUS_OK = Sets.newHashSet(new Long[] { ERROR_UNAUTHENTICATED.code });
        HTTP_STATUS_NOT_FOUND = Sets.newHashSet(new Long[] { ERROR_NOT_FOUND.code });
        HTTP_STATUS_UNAUTHORIZED = Sets.newHashSet(new Long[] { ERROR_UNAUTHORIZED.code });
    }

    public static Integer codeToHttpStatus(Long code) {
        Integer httpStatus = null;
        if (HTTP_STATUS_OK.contains(code)) {
            httpStatus = Integer.valueOf(HttpStatus.OK.value());
        } else if (HTTP_STATUS_FORBIDDEN.contains(code)) {
            httpStatus = Integer.valueOf(HttpStatus.FORBIDDEN.value());
        } else if (HTTP_STATUS_NOT_FOUND.contains(code)) {
            httpStatus = Integer.valueOf(HttpStatus.NOT_FOUND.value());
        } else if (HTTP_STATUS_UNAUTHORIZED.contains(code)) {
            httpStatus = Integer.valueOf(HttpStatus.UNAUTHORIZED.value());
        } else {
            httpStatus = Integer.valueOf(HttpStatus.BAD_REQUEST.value());
        }
        return httpStatus;
    }

}
